package pl.edu.agh.iosr.gamblingzone.userpanel.action;

import static org.jboss.seam.ScopeType.EVENT;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;

import pl.edu.agh.iosr.gamblingzone.model.User;

@Stateful
@Scope(EVENT)
@Name("changeUserData")
public class ChangeUserDataAction implements ChangeUserData {

	/** The user. */
	@In
	private User user;

	/** The em. */
	@PersistenceContext
	private EntityManager em;

	/** The faces messages. */
	@In
	private FacesMessages facesMessages;

	private boolean changed;

	public void changeUserData() {
		// it has to be changed a bit
		// user with invalid email should not be allowed to be merged
		// all fields are marked as required
		user = em.merge(user);
		facesMessages.add("User data changed");
		changed = true;
	}

	public boolean isChanged() {
		return changed;
	}

	public void invalid() {
		facesMessages.add("Please try again");
	}

	@Remove
	public void destroy() {
	}
}
